package cn.kooki.app.duobao.ui.Activity.Comm;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.RelativeLayout;

import java.net.URL;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.base.BaseNavActivity;
import cn.kooki.app.duobao.data.constant.Constants;
import cn.kooki.app.duobao.ui.widget.JSBridgeWebView;
import cn.kooki.app.duobao.util.NetWorkUtils;

/**
 * Created by xiong on 2015/12/27.
 */
public class WebViewActivity extends BaseNavActivity implements View.OnClickListener {
    @Bind(R.id.webview)
    JSBridgeWebView webview;
    @Bind(R.id.root)
    RelativeLayout root;

    public static final int QUESTION_TYPE = 1;
    public static final int REUSLT_TYPE = 2;
    public static final int FINDPASS_TYPE = 3;
    public static final int GOOD_DETAIL = 0;
    public static final int NOTICE_TYPE = 4;
    public static final int ABOUT_TYPE = 5;
    private int type = GOOD_DETAIL;
    private String url;

    private boolean enableback = false;

    @Override
    public void setLayoutContainerView() {
        setContainerView(R.layout.activity_web_view);
        ButterKnife.bind(this);
    }

    @Override
    protected void afterView() {
        String title = getIntent().getStringExtra("title");
        setTitle(title);
        type = getIntent().getIntExtra("type", GOOD_DETAIL);
        if (type == GOOD_DETAIL) {
            String id = getIntent().getStringExtra("id");
            String qishu = getIntent().getStringExtra("qishu");
            url = Constants.GOOD_DETAIL_URL + id + "-" + qishu;
        } else if (type == QUESTION_TYPE) {
            setTitle(R.string.index_tab_qa);
            url = Constants.APP_NEWS_URL;
        } else if (type == REUSLT_TYPE) {
            String id = getIntent().getStringExtra("id");
            String qishu = getIntent().getStringExtra("qishu");
            enableback = true;
            url = String.format(Constants.APP_RESULT_DETAIL_URL, id, qishu);
        } else if (type == FINDPASS_TYPE) {
            url = Constants.APP_FINDPASS_URL;
            enableback = true;
        } else if (type == NOTICE_TYPE) {
            url = Constants.APP_NOTICE_URL;
            enableback = true;
            setTitle("通知-去抢宝");
        }else if (type == ABOUT_TYPE){
            url = Constants.APP_HELP_URL;
            setTitle("关于去抢宝");
        }
        topRefresh.setVisibility(View.VISIBLE);
        topRefresh.setOnClickListener(this);
        loadwebview();
        webview.setRetryClickListener(new JSBridgeWebView.onRetryClickListener() {
            @Override
            public void onRetry() {
                loadwebview();
            }
        });
        WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setUseWideViewPort(true);
        if (enableback) {
            topBack.setOnClickListener(this);
        }
    }

    private void loadwebview() {
        if (NetWorkUtils.isNetworkConnected(this)) {
            webview.loadUrl(url);
        } else {
            webview.showNetError();
        }
    }


    @Override
    public void onBackPressed() {
        if (enableback && webview.canGoBack()) {
            webview.goBack();
            return;
        }
        super.onBackPressed();
    }

    @Override
    public void onClick(View v) {
        if (v == topRefresh) {
            loadwebview();
        }
        if (v == topBack) {
            onBackPressed();
        }
    }

}
